﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace DoNet.Common.Utilities
{
    public class MessageBox
    {
        /// <summary>
        ///  在页面中显示提示信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="messContext"></param>
        public static void Show(System.Web.UI.Page page,string messContext)
        {
            string key = "message";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(" alert(' ");
            sb.Append(messContext.Trim());
            sb.Append(" ');");
            Type cstype = page.GetType();
            ClientScriptManager cs = page.ClientScript;
            if (!cs.IsStartupScriptRegistered(key))
            {
                cs.RegisterStartupScript(cstype, key, sb.ToString(), true);
            }
        }

        /// <summary>
        /// 在页面中显示提示信息并跳转到指定页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="messContext"></param>
        /// <param name="gotoUrl"></param>
        public static void Show(System.Web.UI.Page page, string messContext, string gotoUrl)
        {
            string key = "message";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(" alert(' ");
            sb.Append(messContext.Trim());
            sb.Append(" ');");
            sb.Append("window.location.href='"+gotoUrl+"';");
            Type cstype = page.GetType();
            ClientScriptManager cs = page.ClientScript;
            if (!cs.IsStartupScriptRegistered(key))
            {
                cs.RegisterStartupScript(cstype, key, sb.ToString(), true);
            }
        }

        /// <summary>
        /// 在页面中显示提示信息并返回上一页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="messContext"></param>
        /// <param name="gotoUrl"></param>
        public static void Show(System.Web.UI.Page page, string messContext,bool isBack)
        {
            if (isBack)
            {
                string key = "message";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(" alert(' ");
                sb.Append(messContext.Trim());
                sb.Append(" ');");
                sb.Append("history.go(-1);");
                Type cstype = page.GetType();
                ClientScriptManager cs = page.ClientScript;
                if (!cs.IsStartupScriptRegistered(key))
                {
                    cs.RegisterStartupScript(cstype, key, sb.ToString(), true);
                }
            }
            else
            {
                Show(page, messContext);
            }
        }

        /// <summary>
        /// 在页面中显示提示信息并关闭页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="messContext"></param>
        /// <param name="gotoUrl"></param>
        public static void Close(System.Web.UI.Page page, string messContext)
        {
                string key = "message";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(" alert(' ");
                sb.Append(messContext.Trim());
                sb.Append(" ');");
                sb.Append("window.close();");
                Type cstype = page.GetType();
                ClientScriptManager cs = page.ClientScript;
                if (!cs.IsStartupScriptRegistered(key))
                {
                    cs.RegisterStartupScript(cstype, key, sb.ToString(), true);
                }
        }
    }
}
